#include "bezier.h"
#include <QGraphicsScene>
#include <QGraphicsItemAnimation>


Bezier::Bezier(QWidget *parent) :
    QWidget(parent)
{
    this->soIda = true;
}

void Bezier::setPoints(QPointF p1, QPointF p2, QPointF p3, QPointF p4){
    this->p1 = p1;
    this->p2 = p2;
    this->p3 = p3;
    this->p4 = p4;
}

void Bezier::setP1(QPointF p1){
    this->p1 = p1;

}

QPointF Bezier::getP1(){
    return this->p1;
}

void Bezier::setP2(QPointF p2){
    this->p2 = p2;
}

QPointF Bezier::getP2(){
    return this->p2;
}
void Bezier::setP3(QPointF p3){
    this->p3 = p3;
}

QPointF Bezier::getP3(){
    return this->p3;
}
void Bezier::setP4(QPointF p4){
    this->p4 = p4;
}

QPointF Bezier::getP4(){
    return this->p4;
}

void Bezier::desenhaPontos(QGraphicsScene *scene, QPointF p1, QPointF p2, QPointF p3, QPointF p4){
    scene->addEllipse(p1.x()-3,p1.y()-3,6,6,(QPen)Qt::white,QBrush());
    scene->addEllipse(p2.x()-3,p2.y()-3,6,6,(QPen)Qt::white,QBrush());
    scene->addEllipse(p3.x()-3,p3.y()-3,6,6,(QPen)Qt::white,QBrush());
    scene->addEllipse(p4.x()-3,p4.y()-3,6,6,(QPen)Qt::white,QBrush());
}

void Bezier::setSoIda(bool checked){
    this->soIda = checked;
}

void Bezier::desenhaCurva(QGraphicsScene *scene){

    QPointF ant1 = p1;
    QPointF ant2 = p2;
    QPointF ant3 = p1;
    QPointF ant4 = p3;
    QPointF ant5 = p2;
    QPointF ant6 = p1;

    for (float t = 0.01; t < 1.001; t+= 0.01)
    {
        QPointF temp1 = (1-t)*p1 + t*p2;
        QPointF temp2 = (1-t)*p2 + t*p3;
        QPointF temp3 = (1-t)*temp1 + t*temp2;
        QPointF temp4 = (1-t)*p3 + t*p4;
        QPointF temp5 = (1-t)*temp2 + t*temp4;
        QPointF temp6 = (1-t)*temp3 + t*temp5;

        scene->addLine(ant6.x(), ant6.y(), temp6.x(), temp6.y(), (QPen)Qt::white);

        ant1 = temp1;
        ant2 = temp2;
        ant3 = temp3;
        ant4 = temp4;
        ant5 = temp5;
        ant6 = temp6;
    }
}

void Bezier::percorrerCurva(QGraphicsItemAnimation *animacao){

    QPointF ant1 = p3;
    QPointF ant2 = p2;
    QPointF ant3 = p3;
    QPointF ant4 = p3;
    QPointF ant5 = p2;
    QPointF ant6 = p1;

    for (float t = 0.01; t < 1.001; t+= 0.01)
    {
        QPointF temp1 = (1-t)*p1 + t*p2;
        QPointF temp2 = (1-t)*p2 + t*p3;
        QPointF temp3 = (1-t)*temp1 + t*temp2;
        QPointF temp4 = (1-t)*p3 + t*p4;
        QPointF temp5 = (1-t)*temp2 + t*temp4;
        QPointF temp6 = (1-t)*temp3 + t*temp5;

        animacao->setPosAt(t/200.0, QPointF(temp6.x(),temp6.y()));
        animacao->setRotationAt(t/200.0, 90 - QLineF(temp6.x(), temp6.y(), ant6.x(), ant6.y()).angle());

        if(!soIda){
           animacao->setPosAt((2-t)/200.0, QPointF(temp6.x(),temp6.y()));
           animacao->setRotationAt((2-t)/200.0, 90 - QLineF(temp6.x(), temp6.y(), ant6.x(), ant6.y()).angle());
        }


        ant1 = temp1;
        ant2 = temp2;
        ant3 = temp3;
        ant4 = temp4;
        ant5 = temp5;
        ant6 = temp6;

   }

}

